#ifndef USER_H
#define USER_H

#include <QAbstractListModel>
#include <QList>

class User : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(QHash<QString,QVariant> currentLogin READ currentLogin NOTIFY currentLoginChanged)

public:
    explicit User(QObject *parent = nullptr);

    // QAbstractListModel 接口
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;

    // 添加一行
    Q_INVOKABLE void addRecord(const QString &name, const QString password, const QString &real_name, const QString &expiration, const QString &status, const QString &permission,
                               const QString &create_by);

    // 删除一行
    Q_INVOKABLE void removeRow(int row);

    // 修改一行
    Q_INVOKABLE void updateRow(int row, const QVariantMap &newData);

    // 获取总行数
    Q_INVOKABLE int count() const;
    
    // 获取指定行的数据（模拟 ListModel 的 get 方法）
    Q_INVOKABLE QVariantMap get(int row) const;
    
    // 设置指定行的属性（模拟 ListModel 的 setProperty 方法）
    Q_INVOKABLE void setProperty(int row, const QString &role, const QVariant &value);

    // 批量初始化（加载你提供的16条数据）
    Q_INVOKABLE void loadUser(const QString &filter);
    Q_INVOKABLE QHash<QString,QVariant> login(const QString &name, const QString &password);
    Q_INVOKABLE bool modifyPassword(const QString& password);
    Q_INVOKABLE bool isFirstLogin();
    Q_INVOKABLE bool hasBeenUsed(int count, const QString& password);

signals:
    void modelChanged();
    void currentUserChanged();
    void isLoggedInChanged();
    void currentPermissionChanged();
    void currentLoginChanged();

public slots:
    // 清空所有数据
    void clear();

private:
    QList<QHash<QString,QVariant>> m_records;
    QHash<int, QByteArray> m_roles;
    QHash<QString,QVariant> m_login;
    bool m_isLoggedIn = false;
    int m_currentPermission = 3; // 默认查看员
public:

    bool isLoggedIn() const { return m_isLoggedIn; }
    void setIsLoggedIn(bool v) {
        if (m_isLoggedIn == v) return;
        m_isLoggedIn = v;
        emit isLoggedInChanged();
    }
    int currentPermission() const { return m_currentPermission; }
    void setCurrentPermission(int p) {
        if (m_currentPermission == p) return;
        m_currentPermission = p;
        emit currentPermissionChanged();
    }

    QHash<QString,QVariant> currentLogin(){ return m_login;}
};

#endif // USER_H
